package com.wgtp.ssapp.view.activity;

import java.util.ArrayList;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ListView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.wgtp.ssapp.R;
import com.wgtp.ssapp.model.Forum;
import com.wgtp.ssapp.model.SubForum;
import com.wgtp.ssapp.model.adapters.ForumListAdapter;
import com.wgtp.ssapp.model.sideMenu.SideMenuView;
import com.wgtp.ssapp.model.sideMenu.iSideMenuCallback;

public class ForumListActivity extends SherlockActivity implements iSideMenuCallback {

	private String TAG = "ForumListActivity";
    private static ArrayList<Forum> forumList;
    private static ArrayList<ArrayList<SubForum>> subForumList;
    private ListView forumListView;
    private ForumListAdapter forumListAdapter;
    private SideMenuView sideMenu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate method starting.");
    	
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_page);
        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        
        forumList = new ArrayList<Forum>();
	    subForumList = new ArrayList<ArrayList<SubForum>>();
	    sideMenu = (com.wgtp.ssapp.model.sideMenu.SideMenuView) findViewById(R.id.forum_list_side_menu);
	    sideMenu.setMenuClickCallback(this);
	    sideMenu.setMenuItems(R.menu.ribbon_menu);
        
        setUp();

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        getSupportActionBar().setTitle(getResources().getString(R.string.forum_name_1));
        
        forumListView = (ListView) findViewById(R.id.forum_list_view);
        forumListAdapter = new ForumListAdapter(this, R.layout.forum_list_item_row, subForumList.get(0));
        forumListView.setAdapter(forumListAdapter);
        
		Log.i(TAG, "onCreate method ending.");
    }
    

    public void setUp() {
        ArrayList<Forum> forums = new ArrayList<Forum>();
        
        forums.add(new Forum(getResources().getString(R.string.forum_name_1)));
        forums.add(new Forum(getResources().getString(R.string.forum_name_2)));
        forums.add(new Forum(getResources().getString(R.string.forum_name_3)));
        forums.add(new Forum(getResources().getString(R.string.forum_name_4)));
        forums.add(new Forum(getResources().getString(R.string.forum_name_5)));
        
        ArrayList<ArrayList<SubForum>> subForums = new ArrayList<ArrayList<SubForum>>();
        subForums.add(arrayToArrayList(getResources().getStringArray(R.array.forum_group_1), getResources().getStringArray(R.array.forum_descriptions_1)));
        subForums.add(arrayToArrayList(getResources().getStringArray(R.array.forum_group_2), getResources().getStringArray(R.array.forum_descriptions_2)));
        subForums.add(arrayToArrayList(getResources().getStringArray(R.array.forum_group_3), getResources().getStringArray(R.array.forum_descriptions_3)));
        subForums.add(arrayToArrayList(getResources().getStringArray(R.array.forum_group_4), getResources().getStringArray(R.array.forum_descriptions_4)));
        subForums.add(arrayToArrayList(getResources().getStringArray(R.array.forum_group_5), getResources().getStringArray(R.array.forum_descriptions_5)));
        
        forumList = forums;
        subForumList = subForums;
    }
    
    
    public ArrayList<SubForum> arrayToArrayList(String[] namesArray, String[] descriptionsArray) {
        ArrayList<SubForum> subForums = new ArrayList<SubForum>();
        
        for(int i=0; i<namesArray.length; i++) {
                subForums.add(new SubForum(namesArray[i], descriptionsArray[i]));
        }
        
        return subForums;
    }


	@Override
	public void SideMenuItemClick(int position) {
		getSupportActionBar().setTitle(sideMenu.getMenuItems().get(position).getText());
		forumListAdapter.setForumListData(subForumList.get(position));
		forumListAdapter.notifyDataSetChanged();
		sideMenu.toggleMenu();
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == android.R.id.home) {
			sideMenu.toggleMenu();
			return true;
		} 
		else {
			return super.onOptionsItemSelected(item);
		}
	}
    
}
